package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

import java.util.Scanner;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();
    Scanner sc = new Scanner(System.in);

    public boolean addTeacher(Teacher stn) {
        return teacherDao.addTeacher(stn);
    }

    public Teacher[] findAllTeacher() {
        boolean flog = false;
        for (int i = 0; i < teacherDao.findAllTeacher().length; i++) {
            if (teacherDao.findAllTeacher()[i] != null) {
                flog = true;
                break;
            }
        }
        if (flog) {
            return teacherDao.findAllTeacher();
        } else return null;
    }

    public boolean isExists(String id) {
        boolean flog = false;
        for (Teacher teacher : teacherDao.findAllTeacher()) {
            if (teacher != null && teacher.getId().equals(id)) {
                flog = true;
                break;
            }
        }
        return flog;
    }

    public boolean removeTeacher(String id) {
        int index = isIndex(id);
        boolean flog = false;
        if (index != -1) {
            teacherDao.removeTeacher(index);
            flog = true;
        }
        return flog;
    }

    public int isIndex(String id) {
        int index = -1;
        for (int i = 0; i < teacherDao.findAllTeacher().length; i++) {
            if (teacherDao.findAllTeacher()[i] != null && teacherDao.findAllTeacher()[i].getId().equals(id)) {
                index = i;
            }
        }
        return index;
    }

    public boolean changeTeacher(String id) {
        boolean flog = false;
        int index = isIndex(id);
        if (index != -1) {
            System.out.println("请输入添加的姓名");
            String name = sc.next();
            System.out.println("请输入添加的年龄");
            String age = sc.next();
            System.out.println("请输入添加的生日");
            String birthday = sc.next();
            Teacher stn = new Teacher();
            stn.setName(name);
            stn.setAge(age);
            stn.setBirthday(birthday);
            teacherDao.changeTeacher(stn, index);
            flog = true;
        }
        return flog;
    }
}